//
//  MediaViewController.m
//  HorizonLight
//
//  Created by lanou on 15/9/19.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "MediaViewController.h"
#import "VideoListModel.h"
#import "MediaView.h"
@interface MediaViewController ()

@property (nonatomic, strong) MediaView *mediaView;
//时间数组
@property (nonatomic, strong) NSMutableArray *timerArray;
@end

@implementation MediaViewController
#pragma mark -----懒加载----
-(NSMutableArray *)timerArray
{
    if (!_timerArray)
    {
        _timerArray = [[NSMutableArray alloc] init];
    }
    return _timerArray;
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor clearColor];
    self.moviePlayer = [[SunnyMoviePlayer alloc] initWithContentURL:[NSURL URLWithString:self.mediaUrl]];
    [self.moviePlayer.view setFrame:CGRectMake(0, 0, kScreenHight, kScreenWidth)];
    //适合的缩放比例
//    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:self.mediaUrl]];
    [self.moviePlayer.view setFrame:CGRectMake(0, 0, kScreenHeight, kScreenWidth)];
    self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
    //取消自带的播放按钮
    self.moviePlayer.controlStyle = NO;
    [self.view addSubview:[self.moviePlayer view]];
    [self.moviePlayer play];
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(sliderProress) userInfo:nil repeats:YES];
    //调用
    [self setUpMediaViewAttribute];
    //MPMoviePlayerController视频播放扬声器没有声音,插上耳机才能听到声音,因此设置下面方法解决
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  
}
- (void)sliderProress
{
//    self.mediaView.progress.progress = self.moviePlayer.playableDuration;
//    self.mediaView.slider.value = self.moviePlayer.playa;
}
- (void)audioManagerStreamerProgress:(float)progress
{
//    [self.mediaView.progress setProgress:timezone animated:NO];
    

}
//当前viewcontroller是否支持转屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

//当前viewcontroller支持哪些转屏方向
//- (NSUInteger)supportedInterfaceOrientations
//{
//    return UIInterfaceOrientationLandscapeLeft;
//}

//当前viewcontroller默认的屏幕方向(左边)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}
//计算当前时间和总时间
-(NSString *)setTimeWithInterval:(float)interval
{
    int minutes = interval / 60;
    int seconds = (int)interval % 60;
    return [NSString stringWithFormat:@"%d:%02d",minutes,seconds];
}

-(void)setUpMediaViewAttribute
{
    //初始化UIView
    self.mediaView = [[MediaView alloc] initWithFrame:CGRectMake(0, 0, kScreenHight, kScreenWidth)];
    self.mediaView.titleLabel.text = self.headline;
    //按钮的点击方法
    [self.mediaView.backBtn addTarget:self action:@selector(backClick) forControlEvents:(UIControlEventTouchUpInside)];

    [self.mediaView.shareBtn addTarget:self action:@selector(shareClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.mediaView.clickZanBtn addTarget:self action:@selector(likeClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.mediaView.screenshotBtn addTarget:self action:@selector(screenshotBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.mediaView.seleteBtn addTarget:self action:@selector(seleteBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.mediaView.nextBtn addTarget:self action:@selector(nextBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.mediaView];
    //返回按钮
    self.backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.backBtn.backgroundColor = [UIColor redColor];
    self.backBtn.frame = CGRectMake(20, 10, kIconWidth, kIconHeight);
    [self.backBtn addTarget:self action:@selector(backClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.backBtn];
    
    //标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.backBtn.right + kDistance, 10, kScreenHeight / 2, kIconHeight)];
//    self.titleLabel.backgroundColor = [UIColor greenColor];
    self.titleLabel.text = self.headline;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:17.0];
    [self.view addSubview:self.titleLabel];
    

    //分享按钮
    self.shareBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.shareBtn.frame = CGRectMake(kScreenHeight - kIconWidth - 20 ,10,  kIconWidth, kIconHeight);
    self.shareBtn.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.shareBtn];
    
    //点赞按钮
    self.clickZanBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.clickZanBtn.frame = CGRectMake(_shareBtn.left - kIconWidth - kDistance, 10, kIconWidth, kIconHeight);
    self.clickZanBtn.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.clickZanBtn];
    
    //截图
    self.screenshotBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.screenshotBtn.frame = CGRectMake(_clickZanBtn.left - kIconWidth - kDistance, 10, kIconWidth, kIconHeight);
    self.screenshotBtn.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.screenshotBtn];
    
    //筛选高清
    self.seleteBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.seleteBtn.frame = CGRectMake(_screenshotBtn.left - kIconWidth - kDistance, 10, kIconWidth, kIconHeight);
    self.seleteBtn.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.seleteBtn];
    
    //下一个
    self.nextBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.nextBtn.frame = CGRectMake(kScreenHeight - kIconWidth - 40, (kScreenWidth - kIconHeight ) / 2, 40, 40);
    self.nextBtn.backgroundColor =[UIColor purpleColor];
    [self.view addSubview:self.nextBtn];
    
}
//返回上一页
-(void)backClick
{
    [self.moviePlayer stop];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)shareClick
{

}
-(void)likeClick
{
}
-(void)screenshotBtnClick
{

}
-(void)seleteBtnClick
{

}
-(void)nextBtnClick
{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
